import {config} from '../config'
import {getToken} from '../utils/localstorg'
import axios from 'axios'
const tips={
  1:"抱歉，出现了错误"
}

class HTTP{

  request({url,data={},method='GET'}){
    return new Promise((resolve,reject)=>{
      this._request(url,resolve,reject,data,method)
    })
  }
  _request(url,resolve,reject,data={},method='GET'){
    axios({
      url:config.api_base_url+url,
      headers: {
        'token': getToken()
      },
      method,
      data
    }).then(res=>{
      if(res.status===200){
        resolve(res.data)
      }else{
        reject("后台错误")
        this._show_error(res.status)
      }
    }).catch(err=>{
      reject(err)
      this._show_error(1)
    })
  }

  _show_error(error_code){
    if(!error_code){
      error_code=1
    }
    const tip=tips[error_code]
    console.log(tip);
  }
}

export {HTTP}
